/***************************************************************************
 *   Copyright (C) 2009 by Marian Skvarek   *
 *   skvarek.marian@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "CDialogSetup.h"
#include "CScoreSettings.h"
#include "CGlobals.h"
#include "CInstrument.h"
#include "CFraction.h"

#include <QtGui>

using namespace easyscore::core::utils;
using namespace easyscore::core;

namespace easyscore
{
	namespace gui
	{

		CDialogSetup::CDialogSetup ( QWidget *parent ) : QDialog ( parent )
		{
			setupUi ( this );

			mScoreSettings = 0;

			connect ( okBttn,SIGNAL ( clicked() ),this, SLOT ( onOKButtonClicked() ) );
		}

		void CDialogSetup::setupUi(QDialog *DialogSetup)
		{
			if (DialogSetup->objectName().isEmpty())
				DialogSetup->setObjectName(QString::fromUtf8("DialogSetup"));
			DialogSetup->setWindowModality(Qt::WindowModal);
                        DialogSetup->resize(420, 560);
			NewProjectProperties = new QGroupBox(DialogSetup);
			NewProjectProperties->setObjectName(QString::fromUtf8("NewProjectProperties"));
			NewProjectProperties->setEnabled(true);
			NewProjectProperties->setGeometry(QRect(10, 40, 411, 441));
			songTitleLineEdit = new QLineEdit(NewProjectProperties);
			songTitleLineEdit->setObjectName(QString::fromUtf8("songTitleLineEdit"));
                        songTitleLineEdit->setGeometry(QRect(120, 40, 291, 20));
			TS44RadioBttn = new QRadioButton(NewProjectProperties);
			TS44RadioBttn->setObjectName(QString::fromUtf8("TS44RadioBttn"));
			TS44RadioBttn->setGeometry(QRect(10, 160, 84, 18));
			TS44RadioBttn->setChecked(true);
			TS68RadioBttn = new QRadioButton(NewProjectProperties);
			TS68RadioBttn->setObjectName(QString::fromUtf8("TS68RadioBttn"));
			TS68RadioBttn->setGeometry(QRect(220, 190, 84, 18));
			TS74RadioBttn = new QRadioButton(NewProjectProperties);
			TS74RadioBttn->setObjectName(QString::fromUtf8("TS74RadioBttn"));
			TS74RadioBttn->setGeometry(QRect(110, 190, 84, 18));
			TS54RadioBttn = new QRadioButton(NewProjectProperties);
			TS54RadioBttn->setObjectName(QString::fromUtf8("TS54RadioBttn"));
			TS54RadioBttn->setGeometry(QRect(10, 190, 84, 18));
			TS64RadioBttn = new QRadioButton(NewProjectProperties);
			TS64RadioBttn->setObjectName(QString::fromUtf8("TS64RadioBttn"));
			TS64RadioBttn->setGeometry(QRect(320, 160, 84, 18));
			TS34RadioBttn = new QRadioButton(NewProjectProperties);
			TS34RadioBttn->setObjectName(QString::fromUtf8("TS34RadioBttn"));
			TS34RadioBttn->setGeometry(QRect(220, 160, 84, 18));
			TS24RadioBttn = new QRadioButton(NewProjectProperties);
			TS24RadioBttn->setObjectName(QString::fromUtf8("TS24RadioBttn"));
			TS24RadioBttn->setGeometry(QRect(110, 160, 84, 18));
			TS78RadioBttn = new QRadioButton(NewProjectProperties);
			TS78RadioBttn->setObjectName(QString::fromUtf8("TS78RadioBttn"));
			TS78RadioBttn->setGeometry(QRect(320, 190, 84, 18));
			tempoSlider = new QSlider(NewProjectProperties);
			tempoSlider->setObjectName(QString::fromUtf8("tempoSlider"));
			tempoSlider->setGeometry(QRect(220, 390, 160, 21));
			tempoSlider->setMaximum(300);
			tempoSlider->setValue(60);
			tempoSlider->setOrientation(Qt::Horizontal);
			tempoSpinBox = new QSpinBox(NewProjectProperties);
			tempoSpinBox->setObjectName(QString::fromUtf8("tempoSpinBox"));
			tempoSpinBox->setGeometry(QRect(10, 390, 181, 22));
			tempoSpinBox->setMaximum(300);
			tempoSpinBox->setValue(60);
			authorNameLineEdit = new QLineEdit(NewProjectProperties);
			authorNameLineEdit->setObjectName(QString::fromUtf8("authorNameLineEdit"));
                        authorNameLineEdit->setGeometry(QRect(120, 70, 291, 20));
			songTitleLabel = new QLabel(NewProjectProperties);
			songTitleLabel->setObjectName(QString::fromUtf8("songTitleLabel"));
			songTitleLabel->setGeometry(QRect(10, 40, 81, 16));
			authorNameLabel = new QLabel(NewProjectProperties);
			authorNameLabel->setObjectName(QString::fromUtf8("authorNameLabel"));
                        authorNameLabel->setGeometry(QRect(10, 70, 100, 16));
			setTempoLabel = new QLabel(NewProjectProperties);
			setTempoLabel->setObjectName(QString::fromUtf8("setTempoLabel"));
			setTempoLabel->setGeometry(QRect(10, 360, 81, 16));
			setTimeLabel = new QLabel(NewProjectProperties);
			setTimeLabel->setObjectName(QString::fromUtf8("setTimeLabel"));
                        setTimeLabel->setGeometry(QRect(10, 130, 100, 16));
			chooseInstrumentsLabel = new QLabel(NewProjectProperties);
			chooseInstrumentsLabel->setObjectName(QString::fromUtf8("chooseInstrumentsLabel"));
			chooseInstrumentsLabel->setGeometry(QRect(10, 250, 131, 16));
			vocalChBox = new QCheckBox(NewProjectProperties);
			vocalChBox->setObjectName(QString::fromUtf8("vocalChBox"));
			vocalChBox->setGeometry(QRect(10, 280, 72, 18));
			vocalChBox->setChecked(true);
                        pianoChBox = new QCheckBox(NewProjectProperties);
                        pianoChBox->setObjectName(QString::fromUtf8("pianoChBox"));
                        pianoChBox->setGeometry(QRect(150, 280, 72, 18));
                        pianoChBox->setChecked(true);
                        guitarChBox = new QCheckBox(NewProjectProperties);
                        guitarChBox->setObjectName(QString::fromUtf8("guitarChBox"));
                        guitarChBox->setGeometry(QRect(280, 280, 110, 18));
                        guitarChBox->setChecked(true);
			backGuitarChBox = new QCheckBox(NewProjectProperties);
			backGuitarChBox->setObjectName(QString::fromUtf8("backGuitarChBox"));
                        backGuitarChBox->setGeometry(QRect(10, 310, 101, 18));
                        backGuitarChBox->setChecked(false);
                        backGuitarChBox->setVisible(false);
			bassChBox = new QCheckBox(NewProjectProperties);
			bassChBox->setObjectName(QString::fromUtf8("bassChBox"));
                        bassChBox->setGeometry(QRect(10, 310, 72, 18));
			bassChBox->setChecked(true);
			drumsChBox = new QCheckBox(NewProjectProperties);
			drumsChBox->setObjectName(QString::fromUtf8("drumsChBox"));
                        drumsChBox->setGeometry(QRect(150, 310, 72, 18));
			drumsChBox->setChecked(true);
			okBttn = new QPushButton(DialogSetup);
			okBttn->setObjectName(QString::fromUtf8("okBttn"));
			okBttn->setGeometry(QRect(340, 510, 75, 23));
                        QWidget::setTabOrder(songTitleLineEdit, authorNameLineEdit);
                        QWidget::setTabOrder(authorNameLineEdit, TS44RadioBttn);
			QWidget::setTabOrder(TS44RadioBttn, TS24RadioBttn);
			QWidget::setTabOrder(TS24RadioBttn, TS34RadioBttn);
			QWidget::setTabOrder(TS34RadioBttn, TS64RadioBttn);
			QWidget::setTabOrder(TS64RadioBttn, TS54RadioBttn);
			QWidget::setTabOrder(TS54RadioBttn, TS74RadioBttn);
			QWidget::setTabOrder(TS74RadioBttn, TS68RadioBttn);
			QWidget::setTabOrder(TS68RadioBttn, TS78RadioBttn);
			QWidget::setTabOrder(TS78RadioBttn, tempoSpinBox);
			QWidget::setTabOrder(tempoSpinBox, tempoSlider);

			retranslateUi(DialogSetup);
			QObject::connect(tempoSpinBox, SIGNAL(valueChanged(int)), tempoSlider, SLOT(setValue(int)));
			QObject::connect(tempoSlider, SIGNAL(valueChanged(int)), tempoSpinBox, SLOT(setValue(int)));

			QMetaObject::connectSlotsByName(DialogSetup);
		} // setupUi
		
		void CDialogSetup::retranslateUi(QDialog *DialogSetup)
		{
                        DialogSetup->setWindowTitle(QApplication::translate("DialogSetup", "New score settings", 0, QApplication::UnicodeUTF8));
			NewProjectProperties->setTitle(QApplication::translate("DialogSetup", "New Project Settings", 0, QApplication::UnicodeUTF8));
			songTitleLineEdit->setText(QApplication::translate("DialogSetup", "Untitled", 0, QApplication::UnicodeUTF8));
			TS44RadioBttn->setText(QApplication::translate("DialogSetup", "4/4", 0, QApplication::UnicodeUTF8));
			TS68RadioBttn->setText(QApplication::translate("DialogSetup", "6/8", 0, QApplication::UnicodeUTF8));
			TS74RadioBttn->setText(QApplication::translate("DialogSetup", "7/4", 0, QApplication::UnicodeUTF8));
			TS54RadioBttn->setText(QApplication::translate("DialogSetup", "5/4", 0, QApplication::UnicodeUTF8));
			TS64RadioBttn->setText(QApplication::translate("DialogSetup", "6/4", 0, QApplication::UnicodeUTF8));
			TS34RadioBttn->setText(QApplication::translate("DialogSetup", "3/4", 0, QApplication::UnicodeUTF8));
			TS24RadioBttn->setText(QApplication::translate("DialogSetup", "2/4", 0, QApplication::UnicodeUTF8));
			TS78RadioBttn->setText(QApplication::translate("DialogSetup", "7/8", 0, QApplication::UnicodeUTF8));
			authorNameLineEdit->setText(QApplication::translate("DialogSetup", "Unknown author", 0, QApplication::UnicodeUTF8));
			songTitleLabel->setText(QApplication::translate("DialogSetup", "Song Title:", 0, QApplication::UnicodeUTF8));
			authorNameLabel->setText(QApplication::translate("DialogSetup", "Author's Name:", 0, QApplication::UnicodeUTF8));
			setTempoLabel->setText(QApplication::translate("DialogSetup", "Tempo:", 0, QApplication::UnicodeUTF8));
			setTimeLabel->setText(QApplication::translate("DialogSetup", "Time Signature:", 0, QApplication::UnicodeUTF8));
                        chooseInstrumentsLabel->setText(QApplication::translate("DialogSetup", "Instruments:", 0, QApplication::UnicodeUTF8));
			vocalChBox->setText(QApplication::translate("DialogSetup", "Vocal", 0, QApplication::UnicodeUTF8));
			pianoChBox->setText(QApplication::translate("DialogSetup", "Piano", 0, QApplication::UnicodeUTF8));
			backGuitarChBox->setText(QApplication::translate("DialogSetup", "Back guitar", 0, QApplication::UnicodeUTF8));
			guitarChBox->setText(QApplication::translate("DialogSetup", "Lead Guitar", 0, QApplication::UnicodeUTF8));
			bassChBox->setText(QApplication::translate("DialogSetup", "Bass", 0, QApplication::UnicodeUTF8));
			drumsChBox->setText(QApplication::translate("DialogSetup", "Drums", 0, QApplication::UnicodeUTF8));
			okBttn->setText(QApplication::translate("DialogSetup", "Done", 0, QApplication::UnicodeUTF8));
		} // retranslateUi
		
		void CDialogSetup::onOKButtonClicked()
		{
			if(mScoreSettings != 0)
			{
				delete mScoreSettings;
				mScoreSettings = 0;
			}
			
			mScoreSettings = new CScoreSettings();

			mScoreSettings->setAuthorName ( authorNameLineEdit->text() );
			mScoreSettings->setSongName ( songTitleLineEdit->text() );
			mScoreSettings->setTempo ( CTempo ( tempoSpinBox->value() ) );

			if ( TS68RadioBttn->isChecked() )
			{
				mScoreSettings->setTime ( CFraction ( 6,8 ) );
			}

			if ( TS24RadioBttn->isChecked() )
			{
				mScoreSettings->setTime ( CFraction ( 2,4 ) );
			}

			if ( TS34RadioBttn->isChecked() )
			{
				mScoreSettings->setTime ( CFraction ( 3,4 ) );
			}

			if ( TS44RadioBttn->isChecked() )
			{
				mScoreSettings->setTime ( CFraction ( 4,4 ) );
			}

			if ( TS54RadioBttn->isChecked() )
			{
				mScoreSettings->setTime ( CFraction ( 5,4 ) );
			}
			if ( TS64RadioBttn->isChecked() )
			{
				mScoreSettings->setTime ( CFraction ( 6,4 ) );
			}
			if ( TS74RadioBttn->isChecked() )
			{
				mScoreSettings->setTime ( CFraction ( 7,4 ) );
			}
			if ( TS78RadioBttn->isChecked() )
			{
				mScoreSettings->setTime ( CFraction ( 7,8 ) );
			}

			//Instruments
			mScoreSettings->getInstrumentList().clear();

			if ( vocalChBox->isChecked() )
			{
				CInstrument * v = new CInstrument( eInstrumentVocal  );
				mScoreSettings->getInstrumentList().push_back ( v );
			}
			
			if ( pianoChBox->isChecked() )
			{
				CInstrument * v = new CInstrument( eInstrumentPiano  );
				mScoreSettings->getInstrumentList().push_back ( v );
			}
			
			if ( guitarChBox->isChecked() )
			{
				CInstrument * v = new CInstrument( eInstrumentGuitar  );
				mScoreSettings->getInstrumentList().push_back ( v );
			}
			
			if ( backGuitarChBox->isChecked() )
			{
				CInstrument * v = new CInstrument( eInstrumentGuitar  );
				mScoreSettings->getInstrumentList().push_back ( v );
			}
			
			if ( bassChBox->isChecked() )
			{
				CInstrument * v = new CInstrument( eInstrumentBass  );
				mScoreSettings->getInstrumentList().push_back ( v );
			}
			
			if ( drumsChBox->isChecked() )
			{
				CInstrument * v = new CInstrument( eInstrumentDrums  );
				mScoreSettings->getInstrumentList().push_back ( v );
			}

			if ( mScoreSettings->getInstrumentList().empty() )
			{
				QMessageBox::information ( this,tr ( "No instruments selected" ),tr ( "Please, choose at least one instrument" ) );
				return;
			}

			this->accept();
		}

	}//namespace gui
}//namespace easyscore
